package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.domain.vo.FalseClueVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    @Override
    public List<TbClueTrackRecord> getById(Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.getById(clueId);
        return list;
    }

    /**
     * 新增跟踪记录
     * @param tbClueTrackRecord
     * @return
     */
    @Override
    public int save(ClueTrackRecordVo tbClueTrackRecord) {
        String username = SecurityUtils.getUsername();
        Map<String, Object> map = new HashMap<>();
        map.put("createBy", username);
        TbClueTrackRecord tbClueTrackRecord1 = new TbClueTrackRecord();
        org.springframework.beans.BeanUtils.copyProperties(tbClueTrackRecord, tbClueTrackRecord1);
        tbClueTrackRecord1.setParams(map);
        int row = tbClueTrackRecordMapper.save(tbClueTrackRecord1);


        TbClue tbClue = new TbClue();
        //String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClue.setName(tbClueTrackRecord.getName());
        tbClue.setLevel(tbClueTrackRecord.getLevel());
        tbClue.setSubject(tbClueTrackRecord.getSubject());
        tbClue.setUpdateTime(new Date());
        tbClue.setUpdateBy(username);
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        int low = tbClueMapper.updateTbClue(tbClue);
        if (row <= 0 && low <= 0) {
            return 0;
        }
        int hang=row>low?row:low;
        return hang;
    }



}
